#!/usr/bin/perl
#index.cgi

require './network-lib.pl';
&ReadParse();

&header($text{'network_title'}, "", "index", 0, 1);
#print "<hr>\n";

&foreign_require("net", "net-lib.pl");
#@boot = &foreign_call("net", "boot_interfaces");
@actives = &active_interfaces;
print "<form action=edit_interface.cgi method=post>\n";
print "<table class=mode1 border=0 cellspacing=0 cellpadding=1>\n";
print "<tr><td><b>$text{'bonding_status'}</b></td>\n";
print "<td><select name=nettype>";
printf "<option value='NLB' %s>$text{'NLB'}\n",(&check_bond_status() =~ /NLB/)?"selected":"";
printf "<option value='FT' %s>$text{'FT'}\n",(&check_bond_status() =~ /FT/)?"selected":"";
printf "<option value='NB' %s>$text{'NB'}\n", (&check_bond_status() =~ /NB/)?"selected":"";

print "</select></td>\n";
#printf "<td><input type=radio name=nettype value='NLB' %s onclick=''>$text{'NLB'}\n", (&check_bond_status() =~ /NLB/)?"checked":"";
#printf "<input type=radio name=nettype value='FT' %s onclick=''>$text{'FT'}\n", (&check_bond_status() =~ /FT/)?"checked":"";
#printf "<input type=radio name=nettype value='NB' %s onclick=''>$text{'NB'}</td>\n", (&check_bond_status() =~ /NB/)?"checked":"";
print "<td align=left><input type=submit class=button1 value=$text{'submit'} onclick='change_bond_status()'></td></tr>";
#print "<td colspan=5> <br> </td> \n";
print "</table>";
print "<table class=mode2 cellspacing=0 cellpadding=0 width=100%>\n";
print "<td colspan=5> <hr> </td> \n";
print "<tr>";
#print "<input type=hidden name=eth>\n";
print "<td width=94%><table class=mode1 border=0 cellspacing=0 cellpadding=1 width=100%>\n";
print "<tr class=title1> <td><b>$text{'ifcs_name'}</b></td> ",
      "<td><b>$text{'ifcs_type'}</b></td> ",
      "<td><b>$text{'ifcs_ip'}</b></td> ",
      "<td><b>$text{'ifcs_mask'}</b></td> ",
      "<td><b>$text{'ifcs_status'}</b></td> </tr>\n",
#      "<td><b>$text{'ifcs_rx'}</b></td> ",
#      "<td><b>$text{'ifcs_tx'}</b></td> ",
#show bond0
local $n;
local $a;
=xxxxxx
foreach $a (@actives) {
	next if ($a->{'name'} !~ /^bond/);
	$dismode = "class=mode2";
	print "<tr $dismode> <td nowrap>";
	if ($a->{'virtual'} eq "SLAVE") { print "&nbsp;&nbsp;&nbsp;&nbsp;"; }
	if ($a->{'virtual'} ne "SLAVE"){
#	print "<a href=\"\" onclick='edit_eth(\"$a->{'name'}\"); return false'>",
		      &html_escape(&get_adaptername($a->{'irq'})),"</a></td>\n";
		}
	else{
	printf "%s</td>",&get_adaptername($a->{'irq'});	
		}
	printf "<td nowrap>%s</td>\n",$a->{'virtual'} eq "" ? "Ethernet" : " $a->{'virtual'}";
	print "<td nowrap>",$a->{'bootp'} ? $text{'ifcs_bootp'} :
		     $a->{'dhcp'} ? $text{'ifcs_dhcp'} :
		     $a->{'address'} ? &html_escape($a->{'address'}) :
				       $text{'ifcs_auto'},
	      "</td>\n";
	print "<td nowrap>",$a->{'netmask'} ? &html_escape($a->{'netmask'})
				     : $text{'ifcs_auto'},"</td>\n";
	local $adapterstatus;
	$adapterstatus = &check_adapter_status($a->{'up'});
	print "<td nowrap>$adapterstatus</td>\n";
#	print "<td nowrap>$a->{'rx'}</td>";
#	print "<td nowrap>$a->{'tx'}</td>";
	print "</tr>";
	last;
}
=cut
#show slave
foreach $a (@actives) {
	next if ($a->{'name'} =~ /lo/);
	next if ($a->{'virtual'} =~ /SLAVE/);
        $ethname = &get_showname($a->{'name'});
        next if (!$ethname);
#	if($a->{'virtual'} eq "SLAVE")
	if($n%2==0)
	{
		$dismode = "class=mode1";
	}
	else
	{
		$dismode = "class=mode2";
	}
	print "<tr $dismode> <td nowrap>";
#	if ($a->{'virtual'} eq "SLAVE") { print "&nbsp;&nbsp;&nbsp;&nbsp;"; }
#	if ($a->{'virtual'} ne "SLAVE"){
#	print "<a href=\"\" onclick='edit_eth(\"$a->{'name'}\"); return false'>",
#		      &html_escape(&get_adaptername($a->{'irq'})),"</a></td>\n";
	printf "<input type=radio name=eth value='$a->{'name'}' onclick=''>%s</td>\n",&get_showname($a->{'name'});
#		}
#	else{
#	printf "%s</td>",&get_adaptername($a->{'irq'});	
#		}
	printf "<td nowrap>%s</td>\n",$a->{'virtual'} eq "" ? "Ethernet" : " $a->{'virtual'}";
	print "<td nowrap>",$a->{'bootp'} ? $text{'ifcs_bootp'} :
		     $a->{'dhcp'} ? $text{'ifcs_dhcp'} :
		     $a->{'address'} ? &html_escape($a->{'address'}) :
				       $text{'ifcs_auto'},
	      "</td>\n";
	print "<td nowrap>",$a->{'netmask'} ? &html_escape($a->{'netmask'})
				     : $text{'ifcs_auto'},"</td>\n";
	local $adapterstatus;
	$adapterstatus = &check_adapter_status($a->{'up'});
	print "<td nowrap>$adapterstatus</td>\n";
#	print "<td nowrap>$a->{'rx'}</td>";
#	print "<td nowrap>$a->{'tx'}</td>";
	print "</tr>";
	$n++;
	if($a->{'name'} eq "bond0")
	{
		foreach $r (@actives)
		{
			if($r->{'virtual'} eq "SLAVE")
			{
				if($n%2==0)
				{
					$dismode = "class=mode1";
				}
				else
				{
					$dismode = "class=mode2";
				}
				print "<tr $dismode> <td nowrap>";
				print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				printf "%s</td>\n",&get_showname($r->{'name'});
				printf "<td nowrap>%s</td>\n",$r->{'virtual'} eq "" ? "Ethernet" : " $r->{'virtual'}";
				printf "<td></td><td></td><td nowrap>%s</td>\n",&check_adapter_status($r->{'up'});
				print "</tr>";
				$n++;
			}
		}
	}
}
#show others
=xcx
foreach $a (@actives) {
	next if (($a->{'name'} =~ /^bond/)||($a->{'name'} =~ /^lo/)||($a->{'virtual'} =~ /SLAVE/));
	$dismode = "class=mode2";
	print "<tr $dismode> <td nowrap>";
	if ($a->{'virtual'} eq "SLAVE") { print "&nbsp;&nbsp;&nbsp;&nbsp;"; }
	if ($a->{'virtual'} ne "SLAVE"){
	print "<a href=\"\" onclick='edit_eth(\"$a->{'name'}\"); return false'>",
		      &html_escape(&get_adaptername($a->{'irq'})),"</a></td>\n";
		}
	else{
	printf "%s</td>",&get_adaptername($a->{'irq'});	
		}
	printf "<td nowrap>%s</td>\n",$a->{'virtual'} eq "" ? "Ethernet" : " $a->{'virtual'}";
	print "<td nowrap>",$a->{'bootp'} ? $text{'ifcs_bootp'} :
		     $a->{'dhcp'} ? $text{'ifcs_dhcp'} :
		     $a->{'address'} ? &html_escape($a->{'address'}) :
				       $text{'ifcs_auto'},
	      "</td>\n";
	print "<td nowrap>",$a->{'netmask'} ? &html_escape($a->{'netmask'})
				     : $text{'ifcs_auto'},"</td>\n";
	local $adapterstatus;
	$adapterstatus = &check_adapter_status($a->{'up'});
	print "<td nowrap>$adapterstatus</td>\n";
#	print "<td nowrap>$a->{'rx'}</td>";
#	print "<td nowrap>$a->{'tx'}</td>";
	print "</tr>";
}
=cut
print "</table></td>";
print "<td width=6%><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
print "<tr><td align=right><input type=submit class=button1 value=$text{'infomation'} onclick='show_adapter()'></td></tr>";
print "<tr><td align=right><input type=submit class=button1 value=$text{'configuration'} onclick='config_adapter()'></td></tr>";
print "</table></td></tr>";
print "</table></form>\n";
#print "<a href=\"conf_bond.cgi\">",$text{'bonding_config'},"</a>\n";
#print "<p><hr>\n";
&nas_syslog("network", "naslog_info", 16000, "Get network interfaces infomation");
print <<EOF;
<script>
function edit_eth(name)
{
	document.forms[0].eth.value = name;
	document.forms[0].submit();
}
function change_bond_status()
{
	document.forms[0].action = 'change_bond.cgi';	
}
function show_adapter()
{
	document.forms[0].action = 'show_interface.cgi';	
}
function config_adapter()
{
	document.forms[0].action = 'edit_interface.cgi';	
}
</script>
EOF

&footer("/", $text{'index'});
